<template>
	<view>
		<view class="nav1">
			<u-navbar title="地址管理" bgColor="transparent" :fixed="false" autoBack>
			</u-navbar>
		</view>
		<!-- <view class="">
			<u-notice-bar :text="notice" mode="closable" speed="30" bgColor="#21B9AD" color="#fff"></u-notice-bar>
		</view> -->
		<u-gap height="10"></u-gap>
		<view class="ml30 mr30" v-if="listData.length > 0">
			<view class="mb20 p30 bgFff radius30" v-for="(item,index) in listData" :key="item.id" @click="addClick(item)">
				<view class="f28 c343">{{item.userAddr}} - {{item.houseNumber}}</view>
				<view class="f26 c666 mt10">{{item.realName}} - {{item.phone}}</view>
				<view class="flex-jus mt20">
					<view class="">
						<u-tag v-if="item.isDef == 1" text="默认" size="mini" class="" bgColor="#f7f7f7"
							borderColor="#f7f7f7" color="#111"></u-tag>
					</view>
					<view class="flex">
						<!-- <view class="flex1" v-if="type == 'order'">
							<view class="mr20" v-if="order_id">
								<u-button color="#FFDE6D" shape="circle" :customStyle="{'color':'#34314F','fontSize':'24rpx','height':'60rpx'}"
									@click="updateOrder(item)">选择</u-button>
							</view>
							<view class="mr20" v-else>
								<u-button color="#FFDE6D" shape="circle" :customStyle="{'color':'#34314F','fontSize':'24rpx','height':'60rpx'}"
									@click="updateAddress(item)">选择</u-button>
							</view>
						</view> -->
						<view class="" @click.stop="delBtn(item)">
							<u-icon name="trash" color="#34314F" size="24"></u-icon>
						</view>
						<view class="ml20" @click.stop="openUrl('/subpage/me/addAddress?id='+item.id)">
							<u-icon name="edit-pen" color="#34314F" class="ml20" size="24"></u-icon>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-empty v-else mode="address" text="暂无地址" iconColor="#dcebe5" textColor="#dcebe5" marginTop="50">
		</u-empty>
		<view class="fix-bot">
			<view class="ml30 mr30">
				<u-button color="#21B9AD" :customStyle="{'color':'#fff','height':'80rpx'}" shape="circle"
					@click="openUrl('/subpage/me/addAddress')">新增地址</u-button>
			</view>
			<u-gap height="20"></u-gap>
		</view>
		<u-modal :show="show_action" title="确认删除地址吗？">
			<view class="flex-jus" slot="confirmButton">
				<view class="flex1">
					<view class="mr20">
						<u-button color="#f7f7f7" :customStyle="{'color':'#111','height':'80rpx'}" shape="circle"
							@click="show_action = false">取消</u-button>
					</view>
				</view>
				<view class="flex1">
					<view class="ml20">
						<u-button color="#FFDE6D" :customStyle="{'color':'#111','height':'80rpx'}" shape="circle"
							@click="submitDel">删除</u-button>
					</view>
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		getAddresslist,
		delAddress,
		updateAddress
	} from '@/common/api_user.js'
	import {
		updateOrder
	} from '@/common/api_order.js'
	
	export default {
		name: '',
		data() {
			return {
				notice:'您好！感谢您选择东楚畅通服务。很抱歉，您当前所在区域尚未开通该服务。请重新选择适用于您地区的服务。我们的服务范围目前覆盖：黄石城区、阳新、大冶。所有商品标题均已标注适用区域，选择时请留意查看哦！祝您使用愉快！',
				order_id:'',
				type:'',
				show_action: false,
				sel_address: {},
				listData: []
			}
		},
		onLoad(option) {
			this.order_id = option.order_id || ''
			this.type = option.type || ''
		},
		onShow() {
			this.getList()
		},
		methods: {
			async getList() {
				uni.showLoading()
				const res = await getAddresslist()
				if (res.code == 200) {
					this.listData = res.data
					uni.hideLoading()
				}
			},
			delBtn(item) {
				this.sel_address = item
				this.show_action = true
			},
			async submitDel() {
				this.show_action = false
				uni.showLoading()
				const res = await delAddress({
					id: this.sel_address.id
				})
				if (res.code == 200) {
					uni.hideLoading()
					this.getList()
				}
			},
			addClick(item){
				if(this.type == 'order'){
					if(this.order_id){
						// if (item.userAddr.indexOf('黄石港区') != -1 || item.userAddr.indexOf('下陆区') != -1 || item.userAddr
						// 	.indexOf('西塞山区') != -1) {
						// 		this.updateOrder(item)
						// }else{
						// 	uni.showModal({
						// 		title:'提示',
						// 		content:this.notice,
						// 		showCancel:false
						// 	})
						// }
						this.updateOrder(item)
					}else{
						// if (item.userAddr.indexOf('黄石港区') != -1 || item.userAddr.indexOf('下陆区') != -1 || item.userAddr
						// 	.indexOf('西塞山区') != -1) {
						// 		this.updateAddress(item)
						// }else{
						// 	uni.showModal({
						// 		title:'提示',
						// 		content:this.notice,
						// 		showCancel:false
						// 	})
						// }
						this.updateAddress(item)
					}
				}
			},
			async updateAddress(item){
				let data = {
					id:item.id,
					userAddr:item.userAddr,
					houseNumber: item.houseNumber,
					realName:item.realName,
					userGender:item.userGender,
					phone:item.phone,
					isDef:1
				}
				uni.showLoading({
					mask:true
				})
				const res = await updateAddress(data)
				if (res.code == 200) {
					uni.hideLoading()
					uni.navigateBack()
				}
			},
			async updateOrder(item){
				uni.showLoading({
					mask:true
				})
				const res = await updateOrder({
					id:this.order_id,
					addrId:item.id
				})
				if (res.code == 200) {
					uni.hideLoading()
					uni.navigateBack()
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F1F9F6;
	}
</style>